package com.zbcz.crm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.crm.CrmApplication
import com.zbcz.crm.bean.WinningBidBean
import com.zbcz.crm.network.CrmApi
import com.zbcz.crm.network.RetrofitManager
import com.zbcz.crm.sp.SharedPreferencesManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WinningBidViewModel : ViewModel() {


    val mResult: MutableLiveData<List<WinningBidBean>?> = MutableLiveData()

    private val mApi = RetrofitManager.getRetrofit().create(CrmApi::class.java)

    fun queryWinningBid(id: String?) {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = queryWinningBidSuspend(id)
                mResult.postValue(result)
            }
        }
    }

    private suspend fun queryWinningBidSuspend(
        id: String?
    ): List<WinningBidBean>? {
        try {
            val result = mApi?.queryWinningBidList(
                token = SharedPreferencesManager.getToken(CrmApplication.mApplication),
                id
            )
            if (result?.success == true) {
                return result.result?.records
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


    override fun onCleared() {
        super.onCleared()
    }
}